<template>
  <div>
    <Delagency ref="del"></Delagency>
    <a-card class="general-card">
      <a-table
        row-key="id"
        :loading="loading"
        :data="renderData"
        :bordered="false"
        :pagination="false"
      >
        <template #columns>
          <a-table-column :title="props.position" data-index="title" />
          <a-table-column
            :title="$t('columns.operations')"
            align="center"
            data-index="operations"
            :width="250"
          >
            <template #cell="{ record }">
              <!-- <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record, 'DEL')"
              >
                废除
              </a-button> -->
              <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record, 'SEND')"
              >
                已办
              </a-button>
              <a-button
                type="text"
                size="small"
                @click="handleClickToSave(record, 'DETAIL')"
              >
                详情
              </a-button>
            </template>
          </a-table-column>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import useLoading from '@/hooks/loading';
  import { useUserStore } from '@/store';
  import { useRouter } from 'vue-router';
  import { TableData, Message } from '@arco-design/web-vue';
  import {
    acquireAgencylist,
    QueryToDoDTO,
    TodoVO,
    sendTodoDone,
  } from '@/api/agency';
  import Delagency from './delagency.vue';

  const props = defineProps({
    position: {
      type: String,
      default: 'agency',
    },
  });
  const router = useRouter();
  const UserStore = useUserStore();
  const { loading, setLoading } = useLoading();
  const renderData = ref<TableData[]>([]);
  const positionFormModel = () => {
    return {
      completeFlag: 'UNCOMPLETED', // 状态 NORMAL审批中,DRAFT草稿,DELETED作废,OVER结束
      formId: '', // 表单id 可模糊查询
      title: '', //  标题 可模糊查询
      userId: UserStore.user.id || '', // 待办人编码
      type: 'TODO',
    };
  };
  const sendFormModel = () => {
    return {
      applicationCode: null, // 应用code
      realDealer: UserStore.user.id || null, // 真实处理人
      realDealerName: UserStore.user.name || null, // 真实处理人名称
      taskId: '', // 任务id
    };
  };
  const formModel = ref(positionFormModel());
  const parmas = ref(sendFormModel());
  const fetchData = async (params: QueryToDoDTO = { ...formModel.value }) => {
    setLoading(true);
    try {
      const { data } = await acquireAgencylist(params);
      renderData.value = data;
    } catch (err) {
      // you can report use errorHandler or other
    } finally {
      setLoading(false);
    }
  };
  const del = ref();
  const handleClickToSave = (obj: TodoVO, type: string) => {
    if (type === 'DEL') {
      del.value.handleChangeVisible(obj.formId);
    } else if (type === 'SEND') {
      parmas.value.taskId = obj.taskId;
      sendTodoDone(parmas.value).then(() => {
        Message.success('操作成功');
        fetchData();
      });
    } else if (type === 'DETAIL') {
      const url = ref(obj.path.includes('http'));

      obj.path = `${obj.path}${obj.path.includes('?') ? '&' : '?'}taskId=${
        obj.taskId ? obj.taskId : ''
      }`;
      if (!url.value) {
        obj.path = router.resolve(obj.path).href;
      }
      window.open(obj.path, '_blank');
    }
  };
  fetchData();
</script>

<style scoped lang="less">
  :deep(.arco-table-th) {
    &:last-child {
      .arco-table-th-item-title {
        margin-left: 16px;
      }
    }
  }
</style>
